#region "DevelopmentLibraryEnvironment"

/*
 * ####     Developer Name : Wang Yucai
 * 
 * ####     Development Tool : Microsoft VisualStudio 2010 Ultimate Edition
 * 
 * ####     Create Time : 2014-07-09 11:04:30
 * 
 * ####     Namespace : Masterduner.SourceStudio.Practices.DevelopmentLibrary.Environments
 * 
 * ####     Type Name : DevelopmentLibraryEnvironment
 * 
 * ####     Full Name : Masterduner.SourceStudio.Practices.DevelopmentLibrary.Environments.DevelopmentLibraryEnvironment
 * 
 * ####     Machine Name : GLCHQWYCWINW7
 * 
 * ####     UserName : GUOCOLAND/wangyucai
 * 
 * ####     CLR Version : 4.0.30319.18444
 * 
 * ####     Target Framework Version : 4.0
 */

#endregion

using System;
using System.IO;

namespace Masterduner.SourceStudio.Practices.DevelopmentLibrary.Environments
{
    /// <summary>
    /// <para>Masterduner.SourceStudio.Practices.DevelopmentLibrary.Environments.DevelopmentLibraryEnvironment</para>
    /// <para>
    /// 提供了访问开发库环境信息的方法。
    /// </para>
    /// </summary>
    /// <remarks>
    /// <para>Target Framework Version : 4.0</para>
    /// <para>此类不可继承。</para>
    /// </remarks>
    public sealed class DevelopmentLibraryEnvironment
    {
        private DirectoryInfo _finalLoadPath;
        private DevelopmentLibraryEnvironmentVariable _environmentVariable;
        private const string DevLibsFolderName = @"Wang Yucai\Masterduner SourceStudio\DevelopmentLibrary";
        private DevelopmentLibraryVersion _version;
        private DevelopmentLibraryFileVersion _fileVersion;
        static private DevelopmentLibraryEnvironment _currentEnvironment;
        private bool _developmentLibraryDirectoryNotFound;

        #region DefaultConfigurationFileName
        /// <summary>
        /// 开发库默认配置文件名称。
        /// </summary>
        public const string DefaultConfigurationFileName = "DevLib.Configuration.config";
        #endregion

        #region CurrentEnvironment
        /// <summary>
        /// 获取当前应用的开发库环境信息。
        /// </summary>
        static public DevelopmentLibraryEnvironment CurrentEnvironment
        {
            get
            {
                if (_currentEnvironment.IsNull()) _currentEnvironment = new DevelopmentLibraryEnvironment();
                return _currentEnvironment;
            }
        }
        #endregion

        #region FinalLoadPath
        /// <summary>
        /// 获取开发库最终的加载路径。
        /// </summary>
        public DirectoryInfo FinalLoadPath
        {
            get { return _finalLoadPath; }
            private set { _finalLoadPath = value; }
        }
        #endregion

        #region EnvironmentVariable
        /// <summary>
        /// 获取开发库的环境变量信息。
        /// </summary>
        public DevelopmentLibraryEnvironmentVariable EnvironmentVariable
        {
            get { return _environmentVariable; }
            private set { _environmentVariable = value; }
        }
        #endregion

        #region Version
        /// <summary>
        /// 获取开发库版本。
        /// </summary>
        public DevelopmentLibraryVersion Version
        {
            get { return _version; }
            private set { _version = value; }
        }
        #endregion

        #region FileVersion
        /// <summary>
        /// 获取开发库文件版本。
        /// </summary>
        public DevelopmentLibraryFileVersion FileVersion
        {
            get { return _fileVersion; }
            private set { _fileVersion = value; }
        }
        #endregion

        #region DevelopmentLibraryDirectoryNotFound
        /// <summary>
        /// 获取是否无法找到开发库的默认目录路径。
        /// </summary>
        public bool DevelopmentLibraryDirectoryNotFound
        {
            get { return _developmentLibraryDirectoryNotFound; }
            private set { _developmentLibraryDirectoryNotFound = value; }
        }
        #endregion

        #region Constructor

        /// <summary>
        /// <para>构造函数：</para>
        /// <para>初始化一个<see cref="DevelopmentLibraryEnvironment" />对象实例。</para>
        /// </summary>
        private DevelopmentLibraryEnvironment()
        {
            this.DevelopmentLibraryDirectoryNotFound = true;
            this.EnvironmentVariable = new DevelopmentLibraryEnvironmentVariable();
            this.Version = new DevelopmentLibraryVersion();
            this.FileVersion = new DevelopmentLibraryFileVersion();
            bool dirExists = this.InitializeInstalledDirectory();
            if (dirExists) this.DevelopmentLibraryDirectoryNotFound = false;
            else
            {
                dirExists = this.InitializeFixedDirectory();
                if (dirExists) this.DevelopmentLibraryDirectoryNotFound = false;
            }
        }

        #endregion

        #region InitializeInstalledDirectory
        /// <summary>
        /// 首先通过环境变量获取开发库安装目录路径。
        /// </summary>
        /// <returns>如果开发库安装目录路径存在，则返回true；否则返回false。</returns>
        private bool InitializeInstalledDirectory()
        {
            if (this.EnvironmentVariable.EnvironmentVariableNotFound)
            {
                return false;
            }
            else
            {
                string installedPath = this.EnvironmentVariable.InstalledPath;
                if (installedPath.IsEmptyOrWriteSpaces())
                {
                    return false;
                }
                else
                {
                    DirectoryInfo installedDir = new DirectoryInfo(installedPath);
                    DevelopmentLibraryDirectorySearchOrder.AddOrder(installedDir);
                    if (installedDir.Exists) this.FinalLoadPath = installedDir;
                    return installedDir.Exists;
                }
            }
        }
        #endregion

        #region InitializeFixedDirectory
        /// <summary>
        /// 将固定目录路径初始化开发库路径。
        /// </summary>
        /// <returns>如果固定目录路径不存在，则返回false；否则返回true。</returns>
        private bool InitializeFixedDirectory()
        {
            string path = Path.Combine(new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Windows)).Root.FullName, DevLibsFolderName);
            DirectoryInfo fixedDir = new DirectoryInfo(path);
            DevelopmentLibraryDirectorySearchOrder.AddOrder(fixedDir);
            if (fixedDir.Exists) this.FinalLoadPath = fixedDir;
            return fixedDir.Exists;
        }
        #endregion
    }
}

#region README

/*
 * ####     ©2014 Wang Yucai
 * 
 * ####     Contact me
 *                  
 *                  E-mail : Masterduner@Yeah.net
 *                  Mobile : 13621384677
 *                  QQ : 180261899
 */

#endregion